package com.zhao.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhao.service.CoreService;
import com.zhao.service.impl.CoreServiceImpl;
import com.zhao.util.SignUtil;


 
@WebServlet("/coreServlet")
public class CoreServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private CoreService coreService=new CoreServiceImpl();   
 
    public CoreServlet() {
        super();
    }

 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String signature=request.getParameter("signature");
		String timestamp=request.getParameter("timestamp");
		String nonce=request.getParameter("nonce");
		String echostr=request.getParameter("echostr");
		boolean flag=SignUtil.checkSignature(signature, timestamp, nonce);
		if(flag) {
			response.getWriter().print(echostr);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		String signature=request.getParameter("signature");
		String timestamp=request.getParameter("timestamp");
		String nonce=request.getParameter("nonce");
		boolean flag=SignUtil.checkSignature(signature, timestamp, nonce);
		if(flag) {
			String result=coreService.processRequest(request);
			response.getWriter().print(result);
		}
	}

}
